from xmw.dispatcher.event import *

from xmw import error
from xmw.tianya.bot import Bot_Thread
def get_msg_helper(name):
    helper=helper_register.get(name)
    
    if helper is None:
        return None
    else:
        return helper()
    
class Helper:
    def get_param(self,name,post):
        if not post.has_key(name):
            return None
        else:
            return post[name]

    def build_msg(self,post):
        pass
    def build_post(self,msg):
        pass
    
    def handle_msg(self,msg):
        """
            called when to process the msg in db
        """
        pass

class TRACE_Helper(Helper):
    #make Msg:TRACE
    def make_msg(self,url=None,sender=None,thread_id=None,author   =None,post_date=None):
        msg=Msg(sender=sender,url=url,thread_id=thread_id,name=MsgName.NOTIFY_POST)
        return msg
    
    def build_msg(self,post):
        #return Msg(location="127.0.0.1",name =MsgName.TRACE)
        sender=self.get_param('sender', post)
        if sender is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.TRACE,"sender")))
        
        url=self.get_param('url', post)
        if url is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.TRACE,"url")))

        thread_id=self.get_param('thread_id', post)
        if thread_id is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.TRACE,"thread_id")))
        
        msg=self.make_msg(sender=sender,url=url,thread_id=thread_id)
        
        return (msg,None)

            
    def handle_msg(self,msg):
        """
        in bot,when to handle TRACE,just:
            1.read the msg(done by RequestHandler)
            2.add thread and trace it
            3.turn off the msg    
        """
        urls=[]
        urls.append(msg.url)
        thread=Bot_Thread(thread_id=msg.thread_id,lst_page_url=urls)
        thread.put()
        msg.status=MsgStatus.OFF
        msg.put()

class NOTIFY_POST_Helper(Helper): 
    def make_msg(self,url=None,sender=None,thread_id=None,author=None,date=None,post_id=None):
        msg=Msg(sender=sender,url=url,thread_id=thread_id,name=MsgName.NOTIFY_POST)
        return msg
    
    def build_msg(self,post):
        #return Msg(location="127.0.0.1",name =MsgName.TRACE)
        sender=self.get_param('sender', post)
        if sender is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.NOTIFY_POST,"sender")))
        
        url=self.get_param('url', post)
        if url is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.NOTIFY_POST,"url")))

        thread_id=self.get_param('thread_id', post)
        if thread_id is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.NOTIFY_POST,"thread_id")))
        
        post_id=self.get_param('post_id', post)
        if post_id is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.NOTIFY_POST,"post_id")))
 
        date=self.get_param('date', post)
        if date is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.NOTIFY_POST,"date")))
        
        author=self.get_param('author', post)
        if author is None:
            return (None,error.POST_MSG_PARAM_FAULT("Msg:%s,expected param:%s"%(MsgName.NOTIFY_POST,"author")))
         
        msg=self.make_msg(sender=sender,url=url,thread_id=thread_id,author=author,date=date,post_id=post_id)
        
        return (msg,None)        
    def handle_msg(self,msg):
        """
        in bot,when to handle TRACE,just:
            1.read the msg(done by RequestHandler)
            2.add thread and trace it
            3.turn off the msg    
        """
        urls=[]
        urls.append(msg.url)
        thread=Bot_Thread(thread_id=msg.thread_id,lst_page_url=urls)
        thread.put()
        msg.status=MsgStatus.OFF
        msg.put()  
         
helper_register={MsgName.TRACE:TRACE_Helper,MsgName.NOTIFY_POST:NOTIFY_POST_Helper}
    
if __name__ == "__main__":
    b=TRACE_Helper()
    b.build_msg(None)
    print "ok"
        
    